/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : Basic2DAgent.cpp
 *************************************************************************
 *
 	 Copyright (C) 2011
 	 TODO, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
	FILE COMMENT /// TODO
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/

#include <stdlib.h>
#include "Basic2DGraphicAgent.h"
#include "GameEngine.h"
#include "SDL/SDL.h"

Basic2DGraphicAgent::Basic2DGraphicAgent(Engine* anEngine) : Agent(anEngine)
{
	this->addClassName("Basic2DGraphicAgent");

	SDL_Rect* anSDLRectangle = ((GameEngine*) anEngine)->requestScreenBounds();

	this->xBound = anSDLRectangle->w;
	this->yBound = anSDLRectangle->h;

	coordinates = new SDL_Rect;

	/**
	 * Setting the size of the graphical representation of the agent.
	 */
	coordinates->h = 5;
	coordinates->w = 5;

	/**
	 * Generating random coordinates and direction.
	 */

	generateRandomcoordinates();
	selectRandomDirection();
}

Basic2DGraphicAgent::~Basic2DGraphicAgent()
{
}

Basic2DGraphicAgent::Basic2DGraphicAgent(const Basic2DGraphicAgent& aBasic2DGraphicAgent, Engine* anEngine)
{
	selfCopyFrom(aBasic2DGraphicAgent, anEngine);
	/// FIXME Est-ce que l'agent devrait s'ajouter lui même au système ?
}

void Basic2DGraphicAgent::selfCopyFrom(const Basic2DGraphicAgent& aBasic2DGraphicAgent, Engine* anEngine)
{
	Agent::selfCopyFrom(aBasic2DGraphicAgent, anEngine);

	coordinates->x = aBasic2DGraphicAgent.coordinates->x;
	coordinates->y = aBasic2DGraphicAgent.coordinates->y;
	coordinates->h = aBasic2DGraphicAgent.coordinates->h;
	coordinates->w = aBasic2DGraphicAgent.coordinates->w;

	selectRandomDirection();
}

void Basic2DGraphicAgent::move()
{
	switch(this->direction)
	{
		case TOP:
			coordinates->y -= 2;
			break;

		case BOTTOM:
			coordinates->y += 2;
			break;

		case LEFT:
			coordinates->x -= 2;
			break;

		case RIGHT:
			coordinates->x += 2;
			break;

		default:
			cout << "Wrong direction selected" << endl;
			/// TODO unknown direction exception.
			break;
	}
}

bool Basic2DGraphicAgent::agentIsOutOfBounds()
{
	return ((coordinates->x > (int) xBound) ||
			(coordinates->x < 0)		    ||
			(coordinates->y > (int) yBound) ||
			(coordinates->y < 0));
}

void Basic2DGraphicAgent::selectRandomDirection()
{
	direction = (unsigned int) rand() % 3;
}

void Basic2DGraphicAgent::generateRandomcoordinates()
{
	coordinates->x = (unsigned int) rand() % xBound;
	coordinates->y = (unsigned int) rand() % yBound;
}

void Basic2DGraphicAgent::display()
{
//	GameEngine* engine = (GameEngine*) getParentEngine();
	// TODO use the correct interface engine->requestBasicDisplayFor(coordinates);
}

bool Basic2DGraphicAgent::isEqualTo(const Basic2DGraphicAgent& anAgent) const
{
	/// TODO
	return false;
}

void Basic2DGraphicAgent::live()
{
	cout << "This is Basic 2d Graphic Agent : " << this->getName() << endl;
	move();

	if(agentIsOutOfBounds())
	{
		generateRandomcoordinates();
		selectRandomDirection();
		coordinates->h = 5;
		coordinates->w = 5;
	}

	display();
}
